﻿using System.Collections.Generic;
using System.ServiceModel;
using Resonance.TrainNode.Contracts.Services;

namespace Resonance.TrainNode.ServiceClients
{
	public static class TrainNodeServiceFactory
	{
		private static Dictionary<string, ChannelFactory<ITrainNodeService>> chFactories = new Dictionary<string, ChannelFactory<ITrainNodeService>>();

		public static void RegisterNewNode(string nodeUniqueName, string nodeAddress, string nodePort)
		{
			lock (chFactories)
			{
				NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
				binding.MaxReceivedMessageSize = 10000000;
				ChannelFactory<ITrainNodeService> channelFactory = new ChannelFactory<ITrainNodeService>(
									binding,
									new EndpointAddress(string.Format("net.tcp://{0}:{1}/ResonanceTrainNode",
									nodeAddress, nodePort)));
				chFactories.Add(nodeUniqueName, channelFactory);
			}
		}

		public static ITrainNodeService GetNodeSvc(string nodeUniqueName)
		{
			lock (chFactories)
			{
				return chFactories[nodeUniqueName].CreateChannel();
			}
		}
	}
}
